'use strict';

app.factory('filterUtil', function () {
  return {
    /**
     * 查询运算的枚举类.
     * <p/>
     * LIKE("LK", " like "),
     * LOWERLIKE("LL", " like "),
     * EQUAL("EQ", " = "),
     * NOTEQUAL("NE", " != "),
     * ISNULL("ISN", " is null "),
     * NOTNULL("NN", " is not null "),
     * BETWEEN("BW", " between "),
     * IN("IN", " in "),
     * NOTIN("NI", " not in "),
     * LESS("L", " < "),
     * LESSEQUAL("LE", " <= "),
     * GREAT("G", " > "),
     * GREATEQUAL("GE", " >= ")
     * @param filter
     * @returns {{}}
     */
    parse: function (filter) {
      var res = {};
      for (var key in filter) {
        var val = filter[key];
        if (val && val == '') {
          continue;
        }

        var keyArr = key.split('_');
        var field = keyArr[0];
        var operate = 'EQ';
        if (keyArr.length > 1) {
          operate = keyArr[1];
        }
        switch (operate) {
          case 'EQ':
            res[field] = val;
            break;
          case 'LK':
            res[field] = '%' + val + '%';
            break;
          case 'NE':
            // TODO:
            break;
          case 'ISN':
            break;
          case 'NN':
            break;
          case 'BW':
            break;
          case 'IN':
            break;
          case 'NI':
            break;
          case 'L':
            break;
          case 'LE':
            break;
          case 'G':
            break;
          case 'GE':
            break;
        }
      }
      return res;
    },
    parseSort: function(sort) {
      for(var key in sort) {
        var val = sort[key];
        if(val) {
          return key + ' DESC';
        }else {
          return key + ' ASC';
        }
      }
      return 'createdAt DESC';
    }
  }
});
